package com.calenda.setevent.api;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.androidquery.AQuery;
import com.androidquery.callback.AjaxStatus;
import com.calenda.setevent.Define;
import com.calenda.setevent.api.BaseAjaxCallBack.IAjaxCallbackMMK;
import com.calenda.setevent.entity.GroupEntity;
import com.calenda.setevent.entity.UserEntity;
import com.calenda.setevent.util.DeviceUtil;
import com.calenda.setevent.util.PreferenceUtil;

public class SetEventApi {
	// private final static String CODE_800 = "800";
	// private final static String CODE_810 = "810";
	// private final static String CODE_811 = "811";
	// private final static String CODE_812 = "812";
	// private final static String CODE_813 = "813";
	// private final static String CODE_814 = "814";
	// private final static String CODE_820 = "820";
	// private final static String CODE_821 = "821";
	// private final static String CODE_822 = "822";
	// private final static String CODE_830 = "830";
	// private final static String CODE_831 = "831";
	// private final static String CODE_832 = "832";
	// private final static String CODE_840 = "840";
	// private final static String CODE_841 = "841";
	// private final static String CODE_850 = "850";
	// private final static String CODE_851 = "851";
	// private final static String CODE_860 = "860";
	// private final static String CODE_861 = "861";
	// private final static String CODE_870 = "870";
	// private final static String CODE_871 = "871";
	private final static String CODE_200 = "200";
	// private final static String CODE_880 = "880";
	// private final static String CODE_881 = "881";
	// private final static String CODE_882 = "882";
	// private final static String CODE_883 = "883";

	static final String DEFAULT_MESSAGE = "Đã có lỗi xảy ra vui lòng kiểm tra cài đặt mạng !!!";

	public final static String getErrorMessage(JSONObject jSONObject) {
		if (jSONObject == null)
			return DEFAULT_MESSAGE;
		return jSONObject.optString("description", "");
		// if (CODE_800.equals(code))
		// return "A JSONObject text must begin with '{'!";
		// if (CODE_810.equals(code))
		// return "Username json parser failed!";
		// if (CODE_811.equals(code))
		// return "Username not availability rules!";
		// if (CODE_812.equals(code))
		// return "Username has been exist!";
		// if (CODE_813.equals(code))
		// return "Username does not exist!";
		// if (CODE_820.equals(code))
		// return "Password json parser failed!";
		// if (CODE_821.equals(code))
		// return "Password not availability rules!";getErrorMessage
		// return "Sessionid is invalid!";
		// if (CODE_840.equals(code))
		// return "Email json parser failed!";
		// if (CODE_841.equals(code))
		// return "Email null!";
		// if (CODE_850.equals(code))
		// return "Phone json parser failed !";
		// if (CODE_851.equals(code))
		// return "Phone null!";
		// if (CODE_860.equals(code))
		// return "Device token json parser failed!";
		// if (CODE_861.equals(code))
		// return "Device token null!";
		// if (CODE_870.equals(code))
		// return "Device type json parser failed!";
		// if (CODE_871.equals(code))
		// return "Device type null!";
		// if (CODE_880.equals(code))
		// return "GroupName json parser failed!";
		// if (CODE_881.equals(code))
		// return "GroupName null!";
		// if (CODE_882.equals(code))
		// return "No Public groups!";
		// return "Đã có lỗi xảy ra";
	}

	public final static void register(String username, String password,
			String fullname, String sex, Context ctx,
			final IAjaxCallbackMMK ajaxCallbackMMK) {

		AQuery aQuery = BaseAjaxCallBack.getInstance(ctx);

		BaseAjaxCallBack<JSONObject> ajaxCallBack = new BaseAjaxCallBack<JSONObject>() {
			@Override
			public void async(Context context) {
				// TODO Auto-generated method stub
				super.async(context);
				ajaxCallbackMMK.handler(null, IAjaxCallbackMMK.START);
			}

			@Override
			public void callback(String url, JSONObject object,
					AjaxStatus status) {
				// TODO Auto-generated method stub
				super.callback(url, object, status);

				if (null == object) {
					Log.d("+++++++++++++++++++++++++", "nulllllllllll");
					ajaxCallbackMMK.handler(getErrorMessage(null),
							IAjaxCallbackMMK.ERROR);
					return;
				}

				String code = object.optString("code", "");

				if (CODE_200.equals(code)) {
					ajaxCallbackMMK.handler(null, IAjaxCallbackMMK.COMPLETE);
				} else {
					ajaxCallbackMMK.handler(getErrorMessage(object),
							IAjaxCallbackMMK.ERROR);
				}

				Log.d("+++++++++++++++++++++++++", object.toString());
			}
		};

		JSONObject jsonObject = null;
		try {
			jsonObject = new JSONObject();
			jsonObject.put("username", username);
			jsonObject.put("password", password);
			jsonObject.put("fullname", fullname);
			jsonObject.put("sex", sex);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		aQuery.post(Define.API_REGISTER, jsonObject, JSONObject.class,
				ajaxCallBack);
	}

	public static final void login(String username, String password,
			Context ctx, final IAjaxCallbackMMK ajaxCallbackMMK) {
		AQuery aQuery = BaseAjaxCallBack.getInstance(ctx);

		BaseAjaxCallBack<JSONObject> ajaxCallBack = new BaseAjaxCallBack<JSONObject>() {
			@Override
			public void async(Context context) {
				// TODO Auto-generated method stub
				super.async(context);
				ajaxCallbackMMK.handler(null, IAjaxCallbackMMK.START);
			}

			@Override
			public void callback(String url, JSONObject object,
					AjaxStatus status) {
				// TODO Auto-generated method stub
				super.callback(url, object, status);

				if (null == object) {
					Log.d("+++++++++++++++++++++++++", "nulllllllllll");
					ajaxCallbackMMK.handler(getErrorMessage(null),
							IAjaxCallbackMMK.ERROR);
					return;
				}

				String code = object.optString("code", "");

				if (CODE_200.equals(code)) {
					JSONObject description = object
							.optJSONObject("description");
					UserEntity userEntity = new UserEntity(description);
					ajaxCallbackMMK.handler(userEntity,
							IAjaxCallbackMMK.COMPLETE);
				} else {
					ajaxCallbackMMK.handler(getErrorMessage(object),
							IAjaxCallbackMMK.ERROR);
				}

				Log.d("+++++++++++++++++++++++++", object.toString());
			}
		};

		JSONObject jsonObject = null;
		try {
			jsonObject = new JSONObject();
			jsonObject.put("username", username);
			jsonObject.put("password", password);
			jsonObject.put("devicetoken", DeviceUtil.getDeviceId(ctx));
			jsonObject.put("devicetype", "ANDROID");

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		aQuery.post(Define.API_LOGIN, jsonObject, JSONObject.class,
				ajaxCallBack);
	}

	public static final void logout(Context ctx,
			final IAjaxCallbackMMK ajaxCallbackMMK) {
		AQuery aQuery = BaseAjaxCallBack.getInstance(ctx);

		BaseAjaxCallBack<JSONObject> ajaxCallBack = new BaseAjaxCallBack<JSONObject>() {
			@Override
			public void async(Context context) {
				// TODO Auto-generated method stub
				super.async(context);
				ajaxCallbackMMK.handler(null, IAjaxCallbackMMK.START);
			}

			@Override
			public void callback(String url, JSONObject object,
					AjaxStatus status) {
				// TODO Auto-generated method stub
				super.callback(url, object, status);

				if (null == object) {
					Log.d("nulllllllllll", "nulllllllllll");
					ajaxCallbackMMK.handler(getErrorMessage(null),
							IAjaxCallbackMMK.ERROR);
					return;
				}

				String code = object.optString("code", "");

				if (CODE_200.equals(code)) {
					ajaxCallbackMMK.handler(null, IAjaxCallbackMMK.COMPLETE);
				} else {
					ajaxCallbackMMK.handler(getErrorMessage(object),
							IAjaxCallbackMMK.ERROR);
				}

				Log.d("objectobjectobjectobject", object.toString());
			}
		};

		JSONObject jsonObject = null;
		try {
			String json = PreferenceUtil.getValue(ctx, Define.KEY_USER_ENTITY,
					"");
			Log.d("get user", json);

			if (!TextUtils.isEmpty(json)) {
				JSONObject job = new JSONObject(json);
				UserEntity userEntity = new UserEntity(job);

				jsonObject = new JSONObject();
				jsonObject.put("username", userEntity.getFullname());
				jsonObject.put("sessionid", userEntity.getSessionid());

				Log.d("hixxxxxxxxxxxxxxxxx", jsonObject.toString());
			}

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}

		if (null != jsonObject)
			aQuery.post(Define.API_LOGOUT, jsonObject, JSONObject.class,
					ajaxCallBack);
	}

	public static final void updateProfile(Context ctx,
			final IAjaxCallbackMMK ajaxCallbackMMK) {
		AQuery aQuery = BaseAjaxCallBack.getInstance(ctx);

		BaseAjaxCallBack<JSONObject> ajaxCallBack = new BaseAjaxCallBack<JSONObject>() {
			@Override
			public void async(Context context) {
				// TODO Auto-generated method stub
				super.async(context);
				ajaxCallbackMMK.handler(null, IAjaxCallbackMMK.START);
			}

			@Override
			public void callback(String url, JSONObject object,
					AjaxStatus status) {
				// TODO Auto-generated method stub
				super.callback(url, object, status);

				if (null == object) {
					Log.d("+++++++++++++++++++++++++", "nulllllllllll");
					ajaxCallbackMMK.handler(getErrorMessage(null),
							IAjaxCallbackMMK.ERROR);
					return;
				}

				String code = object.optString("code", "");

				if (CODE_200.equals(code)) {

					ajaxCallbackMMK.handler(null, IAjaxCallbackMMK.COMPLETE);
				} else {
					ajaxCallbackMMK.handler(getErrorMessage(object),
							IAjaxCallbackMMK.ERROR);
				}

				Log.d("+++++++++++++++++++++++++", object.toString());
			}
		};

		JSONObject jsonObject = null;
		try {

			jsonObject = new JSONObject();
			jsonObject.put("username", "anhdd");
			jsonObject.put("sessionid", "");
			jsonObject.put("fullname", "Đỗ Đức Anh");
			jsonObject.put("sex", "Nam");
			jsonObject.put("email", "anhdd@cdit.com.vn");
			jsonObject.put("mobile", "0904079550");

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		aQuery.post(Define.API_UPDATEPROFILE, jsonObject, JSONObject.class,
				ajaxCallBack);
	}

	public static final void createGroup(String groupname, String desc,
			Context ctx, final IAjaxCallbackMMK ajaxCallbackMMK) {
		AQuery aQuery = BaseAjaxCallBack.getInstance(ctx);

		BaseAjaxCallBack<JSONObject> ajaxCallBack = new BaseAjaxCallBack<JSONObject>() {
			@Override
			public void async(Context context) {
				// TODO Auto-generated method stub
				super.async(context);
				ajaxCallbackMMK.handler(null, IAjaxCallbackMMK.START);
			}

			@Override
			public void callback(String url, JSONObject object,
					AjaxStatus status) {
				// TODO Auto-generated method stub
				super.callback(url, object, status);

				if (null == object) {
					Log.d("+++++++++++++++++++++++++", "nulllllllllll");
					ajaxCallbackMMK.handler(getErrorMessage(null),
							IAjaxCallbackMMK.ERROR);
					return;
				}

				String code = object.optString("code", "");

				if (CODE_200.equals(code)) {
					String groupId = object.optString("groupid", "");
					ajaxCallbackMMK.handler(groupId, IAjaxCallbackMMK.COMPLETE);
				} else {
					ajaxCallbackMMK.handler(getErrorMessage(object),
							IAjaxCallbackMMK.ERROR);
				}

				Log.d("+++++++++++++++++++++++++", object.toString());
			}
		};

		JSONObject jsonObject = null;
		try {
			String json = PreferenceUtil.getValue(ctx, Define.KEY_USER_ENTITY,
					"");
			Log.d("get user", json);

			if (!TextUtils.isEmpty(json)) {
				JSONObject job = new JSONObject(json);
				UserEntity userEntity = new UserEntity(job);

				jsonObject = new JSONObject();
				jsonObject.put("username", userEntity.getFullname());
				jsonObject.put("sessionid", userEntity.getSessionid());
				jsonObject.put("groupname", groupname);
				jsonObject.put("description", desc);
			}

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}

		if (null != jsonObject)
			aQuery.post(Define.API_CREATE_GROUP, jsonObject, JSONObject.class,
					ajaxCallBack);
	}

	public static final void getAllGroup(Context ctx,
			final IAjaxCallbackMMK iAjaxCallbackMMK) {
		AQuery aQuery = BaseAjaxCallBack.getInstance(ctx);

		BaseAjaxCallBack<JSONObject> ajaxCallBack = new BaseAjaxCallBack<JSONObject>() {
			@Override
			public void async(Context context) {
				// TODO Auto-generated method stub
				super.async(context);
				iAjaxCallbackMMK.handler(null, IAjaxCallbackMMK.START);
			}

			@Override
			public void callback(String url, JSONObject object,
					AjaxStatus status) {
				// TODO Auto-generated method stub
				super.callback(url, object, status);

				if (null == object) {
					Log.d("+++++++++++++++++++++++++", "nulllllllllll");
					iAjaxCallbackMMK.handler(getErrorMessage(null),
							IAjaxCallbackMMK.ERROR);
					return;
				}

				String code = object.optString("code", "");

				if (CODE_200.equals(code)) {
					JSONObject jsonDescription = object
							.optJSONObject("description");
					JSONArray jsonArray = jsonDescription.optJSONArray("group");

					List<GroupEntity> groupEntities = new ArrayList<GroupEntity>();

					for (int i = 0; i < jsonArray.length(); ++i) {
						GroupEntity groupEntity = new GroupEntity(
								jsonArray.optJSONObject(i));
						groupEntities.add(groupEntity);
					}
					
					iAjaxCallbackMMK.handler(groupEntities,
							IAjaxCallbackMMK.COMPLETE);

				} else {
					iAjaxCallbackMMK.handler(getErrorMessage(object),
							IAjaxCallbackMMK.ERROR);
				}

				Log.d("+++++++++++++++++++++++++", object.toString());
			}
		};

		JSONObject jsonObject = null;
		try {
			String json = PreferenceUtil.getValue(ctx, Define.KEY_USER_ENTITY,
					"");
			Log.d("get user", json);

			if (!TextUtils.isEmpty(json)) {
				JSONObject job = new JSONObject(json);
				UserEntity userEntity = new UserEntity(job);

				jsonObject = new JSONObject();
				jsonObject.put("username", userEntity.getFullname());
				jsonObject.put("sessionid", userEntity.getSessionid());
			}

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}

		if (null != jsonObject)
			aQuery.post(Define.API_GET_ALL_GROUP, jsonObject, JSONObject.class,
					ajaxCallBack);
	}
}
